import requests
from bs4 import BeautifulSoup
import re

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
         'Content-Type':'application/x-www-form-urlencoded'}

code = input('股票代码：')
resp = requests.get('http://money.finance.sina.com.cn/corp/go.php/vFD_ProfitStatement/stockid/' + code +'/ctrl/part/displaytype/4.phtml',headers=headers)
soup = BeautifulSoup(str(resp.content, 'gbk'), 'html.parser')

links = soup.find_all("a", href = re.compile(r'/corp/view/vFD_Finance'))

link_pfefix = 'http://money.finance.sina.com.cn';

def getData(links,targets):
  for link in links:
    # print(link.get_text())
    if link.get_text() in targets:
      innerLink = link_pfefix + link['href']
  resp = requests.get(innerLink, headers=headers)
  soup = BeautifulSoup(str(resp.content, 'gbk'), 'html.parser')
  records = []
  for td in soup.find_all("td", {"style":"text-align:center"}):
    records.append(td.get_text())
  return records[::3][::-1],[float(x.replace(',','')) for x in records[1::3][::-1]] 

dates = getData(links,['一、营业总收入','一、营业收入'])[0]
# print(type(getData(links,['一、营业总收入','一、营业收入'])[1]),getData(links,['一、营业总收入','一、营业收入'])[1])
# print(type(getData(links,['五、净利润'])[1]),getData(links,['五、净利润'])[1])

contents = []
with open('stock.html', 'r', encoding='utf-8') as f:
  while True:
    myStr = f.readline()
    if not myStr:
        break
    contents.append(myStr)

contents[11] = '      var date = ' + str(dates) + ';\r'
contents[12] = '      var businessIncome = ' + str(getData(links,['一、营业总收入','一、营业收入'])[1]) + ';\r'
contents[13] = '      var netProfit = ' + str(getData(links,['五、净利润'])[1]) + ';\r'
contents[14] = '      var title = \'' + code + '\';\r'

with open('stock.html', 'w', encoding='utf-8') as f:
  f.writelines(contents)
# with open('记录.txt','w') as f:
  # f.writelines(str(resp.content, 'gbk'))